package cn.iocoder.yudao.module.blog.controller.admin.component;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.blog.controller.admin.component.vo.*;
import cn.iocoder.yudao.module.blog.dal.dataobject.component.BlogComponentDO;
import cn.iocoder.yudao.module.blog.convert.component.BlogComponentConvert;
import cn.iocoder.yudao.module.blog.service.component.BlogComponentService;

@Tag(name = "管理后台 - 置顶案例")
@RestController
@RequestMapping("/blog/component")
@Validated
public class BlogComponentController {

    @Resource
    private BlogComponentService componentService;

    @PostMapping("/create")
    @Operation(summary = "创建置顶案例")
    @PreAuthorize("@ss.hasPermission('blog:component:create')")
    public CommonResult<Long> createComponent(@Valid @RequestBody BlogComponentCreateReqVO createReqVO) {
        return success(componentService.createComponent(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新置顶案例")
    @PreAuthorize("@ss.hasPermission('blog:component:update')")
    public CommonResult<Boolean> updateComponent(@Valid @RequestBody BlogComponentUpdateReqVO updateReqVO) {
        componentService.updateComponent(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除置顶案例")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('blog:component:delete')")
    public CommonResult<Boolean> deleteComponent(@RequestParam("id") Long id) {
        componentService.deleteComponent(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得置顶案例")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('blog:component:query')")
    public CommonResult<BlogComponentRespVO> getComponent(@RequestParam("id") Long id) {
        BlogComponentDO component = componentService.getComponent(id);
        return success(BlogComponentConvert.INSTANCE.convert(component));
    }

    @GetMapping("/list")
    @Operation(summary = "获得置顶案例列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('blog:component:query')")
    public CommonResult<List<BlogComponentRespVO>> getComponentList(@RequestParam("ids") Collection<Long> ids) {
        List<BlogComponentDO> list = componentService.getComponentList(ids);
        return success(BlogComponentConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得置顶案例分页")
    @PreAuthorize("@ss.hasPermission('blog:component:query')")
    public CommonResult<PageResult<BlogComponentRespVO>> getComponentPage(@Valid BlogComponentPageReqVO pageVO) {
        PageResult<BlogComponentDO> pageResult = componentService.getComponentPage(pageVO);
        return success(BlogComponentConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出置顶案例 Excel")
    @PreAuthorize("@ss.hasPermission('blog:component:export')")
    @OperateLog(type = EXPORT)
    public void exportComponentExcel(@Valid BlogComponentExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<BlogComponentDO> list = componentService.getComponentList(exportReqVO);
        // 导出 Excel
        List<BlogComponentExcelVO> datas = BlogComponentConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "置顶案例.xls", "数据", BlogComponentExcelVO.class, datas);
    }

}
